package pl.koziolekweb.szkola.KGSiteHeaders;

// KG-SiteHeaders/SiteHeaders.java
 
import java.net.*;
import java.io.*;
import java.util.*;
import static java.lang.System.out;

/**
 *  Uzycie:
 *                 java SiteHeaders url[,url]...
 *             lub
 *                 java SiteHeaders
 *
 *  Moze troche potrwac...
 *                                   T.R. Werner, Mar 2006
 */
class SiteHeaders {

    public static void main(String[] args) {
        SiteHeaders sites = new SiteHeaders(args);
        sites.printHeaders("Server");
    }

    static private String[] defaultDomains = {
        "www.google.com",      // Google
        "java.sun.com",        // SUN
        "www.ibm.com",         // IBM
        "www.hp.com",          // Hewlett-Packard
        "www.microsoft.com",   // Microsoft
        "www.snb.ch",          // Swiss National Bank
        "www.db.com",          // Deutche Bank
        "www.lcl.fr",          // Credit Lyonnaise
        "www.pekao.com.pl",    // PKO SA
        "www.pkobp.pl",        // PKO BP
        "www.bgz.pl",          // BGZ
        "www.millenet.pl",     // Millenium
        "www.allegro.pl",      // Allegro
        "www.sejm.gov.pl",     // Sejm
        "www.whitehouse.gov",  // Bialy Dom
        "www.kremlin.ru",      // Kreml
        "www.prezydent.pl",    // Kaczynski
        "www.gazeta.pl",       // GW
        "www.nytimes.com",     // NYT
        "www.playboy.com",     // Playboy
        "www.onet.pl",         // Onet
        "www.wp.pl",           // Wirtualna Polska
        "www.fuw.edu.pl",      // Fizyka UW
        "www.uw.edu.pl",       // UW
        "www.pw.edu.pl",       // PW
        "www.wit.edu.pl",      // WIT
        "www.pjwstk.edu.pl",   // poljap
        "www.youtube.com",     // YouTube
        "www.myspace.com",     // MySpace
    };

    private Map<String,TreeMap<String,String>> map =
        new TreeMap<String,TreeMap<String,String>>();

    private String[] _domains;

    public SiteHeaders() {
        this(defaultDomains);
    }

    public SiteHeaders(String[] domains) {
        if (domains == null || domains.length == 0)
            _domains = defaultDomains;
        else
            _domains = domains;
        initMap();
    }

    public void printHeaders(String header) {
        out.println("\nSzukanie naglowka " +
                            "\"" + header + "\"\n");
        for (String address : map.keySet()) {
            String h = map.get(address).get(header);
            if (h == null) h = "unknown";
            out.printf("%25s: %-40s\n",address,h);
        }
    }

    private void initMap() {
        for (String address : _domains) {
            address = normalize(address);
            URL url = null;
            try {
                url = new URL("http://" + address);
            } catch(MalformedURLException skip_it) {
            }

            try {
                out.print("Laczenie z " + address + "... ");
                URLConnection conn = url.openConnection();

                TreeMap<String,String> m =
                    new TreeMap<String,String>();
                for (int i = 1; ; ++i) {
                    String h = conn.getHeaderField(i);
                    if (h == null) break;
                    m.put(conn.getHeaderFieldKey(i),h);
                }
                if (m.size() > 0) {
                    out.println("OK.");
                    map.put(address,m);
                } else {
                    out.println("no headers returned");
                }
            } catch(IOException e) {
                out.println("failed.");
                continue;
            }
        }
    }

    private String normalize(String address) {
         return address.startsWith("http://")  ?
                address.substring(7) : address;
    }
}
